#include "editoroptiondialog.h"

EditorOptionDialog::EditorOptionDialog(QWidget *parent): QDialog(parent)
{
    this->setFixedSize(380, 300);
    this->setWindowTitle(tr("Editor Options"));
    QVBoxLayout *mainFormLayout = new QVBoxLayout; //定义垂直布局器
    this->setLayout(mainFormLayout);//将mainFormLayout设定为本窗口的布局器

    fontComboBox = new QFontComboBox;
    fontSizeSpinBox = new QSpinBox;
    tabWidthSpinBox = new QSpinBox;
    languageComboBox = new QComboBox;
    iconSizeSpinBox = new QSpinBox;
    encodingComboBox = new QComboBox;

    okButton = new QPushButton(tr("OK")); //定义OK按钮
    defaultButton = new QPushButton(tr("Default")); //定义Default按钮
    cancelButton = new QPushButton(tr("Cancel")); //定义Cancel按钮

    //fontComboBox->setFontFilters(QFontComboBox::MonospacedFonts);
    fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
    fontComboBox->setEditable(false);
    fontComboBox->setWritingSystem(QFontDatabase::Latin);
    fontComboBox->setSampleTextForSystem(QFontDatabase::Latin, " ");

    fontSizeSpinBox->setRange(2, 80);
    fontSizeSpinBox->setValue(12);

    tabWidthSpinBox->setRange(2, 8);
    tabWidthSpinBox->setValue(4);

    mainFormLayout->setContentsMargins(10, 10, 10, 10);
    mainFormLayout->setSpacing(10);

    QHBoxLayout *hLayout1 = new QHBoxLayout;
    hLayout1->addWidget(new QLabel(tr("Font Name:")));
    hLayout1->addWidget(fontComboBox);

    QHBoxLayout *hLayout2 = new QHBoxLayout;
    hLayout2->addWidget(new QLabel(tr("Font Size:   ")));
    hLayout2->addWidget(fontSizeSpinBox);
    hLayout2->addWidget(new QLabel(tr("    Tab Width:")));
    hLayout2->addWidget(tabWidthSpinBox);

    QVBoxLayout *vLayout1 = new QVBoxLayout;
    vLayout1->addLayout(hLayout1);
    vLayout1->addLayout(hLayout2);

    QGroupBox *groupBox1 = new QGroupBox;
    groupBox1->setLayout(vLayout1);

    QFormLayout *formLayout = new QFormLayout;
    formLayout->addRow(tr("Interface Language:"), languageComboBox);
    formLayout->addRow(tr("ToolBar Icon Size:"), iconSizeSpinBox);

    QGroupBox *groupBox2 = new QGroupBox;
    groupBox2->setLayout(formLayout);

    QHBoxLayout *hLayout4 = new QHBoxLayout;
    hLayout4->addWidget(new QLabel(tr("Encoding for file saving:")));
    hLayout4->addWidget(encodingComboBox);

    QGroupBox *groupBox3 = new QGroupBox;
    groupBox3->setLayout(hLayout4);

    QHBoxLayout *hLayout5 = new QHBoxLayout;
    hLayout5->addStretch();
    hLayout5->addWidget(okButton);
    hLayout5->addWidget(defaultButton);
    hLayout5->addWidget(cancelButton);
    hLayout5->addStretch();

    languageComboBox->addItem("Chinese");
    languageComboBox->addItem("English");

    iconSizeSpinBox->setRange(16, 48);
    iconSizeSpinBox->setValue(16);

    encodingComboBox->addItem("System", QStringConverter::System);
    encodingComboBox->addItem("UTF-8", QStringConverter::Utf8);

    mainFormLayout->addWidget(groupBox1);
    mainFormLayout->addWidget(groupBox2);
    mainFormLayout->addWidget(groupBox3);
    mainFormLayout->addLayout(hLayout5);

    connect(okButton, &QPushButton::clicked, this, &EditorOptionDialog::okButtonClicked);//OK按钮点击和处理函数的连接
    connect(defaultButton, &QPushButton::clicked, this, &EditorOptionDialog::defaultButtonClicked);//Default按钮点击和处理函数的连接
    connect(cancelButton, &QPushButton::clicked, this, &EditorOptionDialog::cancelButtonClicked);//Cancel按钮点击和处理函数的连接

    loadData();//加载各控件内容
}

void EditorOptionDialog::showEvent(QShowEvent* event)
{
    loadData();
    QDialog::showEvent(event);
}

void EditorOptionDialog::loadData()
{
    //读取上次保存的编辑器选项内容，显示在编辑器各控件上。
    QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QSettings setting(appDataPath + "/Setting.ini", QSettings::IniFormat);

    QFont font = setting.value("EditorFont", QFont(QStringList{"Consolas"}, 12)).value<QFont>();
    fontComboBox->setCurrentFont(font);

    int fontSize = setting.value("EditorFontSize", 12).toInt();
    if (fontSize < 2) fontSize = 2;
    else if (fontSize > 80) fontSize = 80;
    fontSizeSpinBox->setValue(fontSize);

    int tabSpaceNumber = setting.value("EditorTabWidth", 4).toInt();
    if (tabSpaceNumber < 2) tabSpaceNumber = 2;
    else if (tabSpaceNumber > 8) tabSpaceNumber = 8;
    tabWidthSpinBox->setValue(tabSpaceNumber);

    QString language = setting.value("Language", "Chinese").toString();
    languageComboBox->setCurrentText(language);

    int iconSize = setting.value("IconSize", 28).toInt();
    iconSizeSpinBox->setValue(iconSize);

    bool ok=false;
    int encodingIndex = setting.value("EncodingIndex", 0).toInt(&ok);
    if (!ok) encodingIndex=0;

    if (encodingIndex!=0&&encodingIndex!=1)
        encodingIndex=0;

    encodingComboBox->setCurrentIndex(encodingIndex);
}

void EditorOptionDialog::okButtonClicked()
{
    QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QSettings setting(appDataPath + "/Setting.ini", QSettings::IniFormat);

    setting.setValue("EditorFont", fontComboBox->currentFont());
    setting.setValue("EditorFontSize", fontSizeSpinBox->value());
    setting.setValue("EditorTabWidth", tabWidthSpinBox->value());
    setting.setValue("Language", languageComboBox->currentText());
    setting.setValue("IconSize", iconSizeSpinBox->value());
    setting.setValue("EncodingIndex", encodingComboBox->currentIndex());

    this->accept();//OK按钮点击以后发出Accepted返回值
}

void EditorOptionDialog::defaultButtonClicked()
{
    fontComboBox->setCurrentFont(QFont("Consolas", 12));
    fontSizeSpinBox->setValue(12);
    tabWidthSpinBox->setValue(4);
    languageComboBox->setCurrentText("Chinese");
    encodingComboBox->setCurrentIndex(0);
    iconSizeSpinBox->setValue(24);
}

void EditorOptionDialog::cancelButtonClicked()
{
    this->reject();//Cancel按钮点击以后发出Rejected返回值
}
